﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UFSSWebApp.UFSSservicereference;
using System.Security.Permissions;
using System.IO;
using System.Web.UI;

namespace UFSSWebApp
{
    public class FileReceiver
    {
        public string FileName;
        private byte[] FileData;
        UFSSserviceSoapClient ussc = new UFSSserviceSoapClient();
        public HttpResponse retrieveFile(UserCreds uc, int fileid)
        {
            UFSSFile fileinfo;
            
            

            fileinfo = ussc.getFileInfo(uc, fileid);
            FileName = fileinfo.filename;
            ussc.DownloadFile(uc, fileid, ref FileData);
            string path = fileinfo.hash + "\\" + fileinfo.filename;
            WriteFile(path);
            StringWriter sw = new StringWriter();
            HttpResponse resp = new HttpResponse(sw);
            
            resp.Buffer = true;
            resp.Clear();
            resp.ClearContent();
            resp.ClearHeaders();
            resp.RedirectLocation = "~/default.aspx";
            resp.ContentType = "application/octet-stream";

            resp.AppendHeader("Content-Disposition", "attachment; filename=" + fileinfo.filename);
            Page p = new Page();
            string mappath = p.Server.MapPath(path);
            resp.TransmitFile(Directory.GetCurrentDirectory() + "\\" + path);
            resp.End();
            
            return resp;
            
        }

        public bool WriteFile(string path, bool append = false)
        {
            if (path.Contains('/'))
                path = path.Replace('/', '\\');
            string temp = Directory.GetCurrentDirectory() + "\\";
            string shortpath = path.Remove(path.IndexOf('\\'));

            System.IO.FileInfo finfo = new System.IO.FileInfo(temp + shortpath);
            FileIOPermission fper = new
            FileIOPermission(FileIOPermissionAccess.AllAccess, finfo.Directory.FullName);
            fper.Demand();

            DirectoryInfo di = new DirectoryInfo(temp + shortpath + "\\");
            if (!di.Exists)
                di.Create();
            FileStream fs = new FileStream(Directory.GetCurrentDirectory() + "\\" + shortpath + "\\" + FileName, FileMode.Create);


            try
            {

                if (append)
                {
                    fs = new FileStream(path, FileMode.Append);


                }
                for (int i = 0; i < FileData.Length; i++)
                    fs.WriteByte(FileData[i]);
                fs.Close();
                return true;
            }
            catch
            {
                fs.Close();
                return false;
            }


        }
        public FileReceiver()
        {

        }
        public FileReceiver(string Filename, byte[] buffer)
        {
            FileName = Filename;
            FileData = buffer;
        }
    }
}